<template>
  <div class="login-container">
    <h3 class="tiitle">小圆的后台管理系统</h3>
    <el-tabs v-model="activeName" class="demo-tabs" type="border-card">
      <el-tab-pane name="account">
        <template #label>
          <span>
            <el-icon><User /></el-icon>
            <span>账号登录</span>
          </span>
        </template>
        <login-account ref="accountRef"></login-account>
      </el-tab-pane>
      <el-tab-pane name="phone">
        <template #label>
          <span>
            <el-icon><Iphone /></el-icon>
            <span>手机登录</span>
          </span>
        </template>
        <login-phone></login-phone>
      </el-tab-pane>
    </el-tabs>
    <div class="action">
      <el-checkbox v-model="checked" label="记住密码" />
      <span>忘记密码</span>
    </div>
    <div class="submit-btn">
      <el-button type="primary" round @click="handleLogin" :loading="isLogin">登录</el-button>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import LoginAccount from './components/login-account.vue'
import LoginPhone from './components/login-phone.vue'
import { setItem, removeItem } from '@/utils/storage'
import { useStore } from 'vuex'

export default defineComponent({
  name: 'Login',
  components: {
    LoginAccount,
    LoginPhone
  },
  setup() {
    const store = useStore()
    const activeName = ref('account')
    const accountRef = ref<InstanceType<typeof LoginAccount>>()
    const checked = ref(false)
    const isLogin = ref(false)
    const handleLogin = () => {
      const formRef = accountRef.value?.formRef
      formRef?.validate((flag) => {
        if (!flag) {
          return
        } else {
          isLogin.value = true
          if (checked.value) {
            setItem('name', accountRef.value?.accountForm.name)
            setItem('password', accountRef.value?.accountForm.password)
          } else {
            removeItem('name')
            removeItem('password')
          }
          store.dispatch('login/onLogin', accountRef.value?.accountForm).finally(() => {
            isLogin.value = false
          })
        }
      })
    }
    return {
      activeName,
      checked,
      accountRef,
      isLogin,
      handleLogin
    }
  }
})
</script>

<style scoped lang="less">
.login-container {
  padding: 50px;
  width: 500px;
  height: 500px;
  box-shadow: 0 5px 15px -5px rgba(0, 0, 0, 0.5);
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50px;
  h3 {
    text-align: center;
    margin-bottom: 16px;
  }
  .action {
    padding: 10px 0;
    .flex-between();
    span {
      cursor: pointer;
      color: #409eff;
      font-size: var(--el-font-size-base);
      &:hover {
        color: #67c23a;
        animation: colorDisplay 1s;
        -webkit-animation: colorDisplay 1s;
      }
    }
  }
  .submit-btn {
    text-align: center;
    .el-button {
      width: 80%;
    }
  }
}
@keyframes colorDisplay {
  0% {
    color: #409eff;
  }
  100% {
    color: #67c23a;
  }
}
</style>
